package com.wqd.behavior.service.impl;

import com.wqd.behavior.dtos.FollowBehaviorDto;
import com.wqd.behavior.pojos.ApFollowBehavior;
import com.wqd.behavior.service.ApFollowBehaviorService;
import com.wqd.model.common.dtos.ResponseResult;
import com.wqd.model.common.enums.AppHttpCodeEnum;
import com.wqd.model.user.pojos.ApUser;
import com.wqd.utils.thread.ApThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 用户关注行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {
        if(dto.getArticleId() == null || dto.getAuthorId() == null){
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //获取登录人id
        ApUser apUser = ApThreadLocalUtil.getUser();
        if(apUser == null){
            return ResponseResult.okResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //检查是否是游客身份
        if(apUser.getId() == 0){
            return ResponseResult.okResult(405,"请登录");
        }

        //定义用户的key  相当于用户的关注列表
        String follow_key = "USER_FOLLOW_" + apUser.getId();
        //定义粉丝的key  相当于作者的粉丝列表
        String fans_key = "USER_FANS_" + dto.getAuthorId();
        //判断redis中是否包含key
        Boolean isFollow = redisTemplate.opsForHash().hasKey(follow_key, dto.getAuthorId().toString());
        //如果关注了,而且是进行了关注操作,那就直接返回
        if(isFollow && dto.getOperation() == 0){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //如果是已经关注了,但是进行了取消关注操作,那就删除关注信息
        if (isFollow && dto.getOperation() == 1){
            //删除redis
            redisTemplate.opsForHash().delete(follow_key,dto.getAuthorId().toString());
            redisTemplate.opsForHash().delete(fans_key,apUser.getId().toString());
            //删除mongodb   当前用户关注作者的数据
            Query query = Query.query(Criteria.where("userId").is(apUser.getId()).and("followId").is(dto.getAuthorId()));
            mongoTemplate.remove(query,ApFollowBehavior.class);
        }
        //如果用户没有关注,且执行了关注操作
        if(!isFollow && dto.getOperation() == 0){
            //新增redis
            redisTemplate.opsForHash().put(follow_key,dto.getAuthorId().toString(),"1");
            redisTemplate.opsForHash().put(fans_key,apUser.getId().toString(),"1");
            //新增mongodb
            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            //生成主键,实体类使用@ID注解可以替换这个方式
            apFollowBehavior.setId(ObjectId.get().toHexString());
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setUserId(apUser.getId().longValue());
            apFollowBehavior.setArticleId(dto.getArticleId());
            apFollowBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apFollowBehavior);
        }
        return ResponseResult.okResult("关注成功");
    }
}
